package com.linchong.useroperation.servlet;

import java.io.IOException;
import java.util.HashSet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import com.fasterxml.jackson.databind.ObjectMapper;
import com.linchong.useroperation.entity.User;
import com.linchong.useroperation.service.UserService;

@WebServlet("/UserOperationServlet")
public class UserOperationServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public static HashSet<User> list = UserService.list();

	public UserOperationServlet() {
		super();
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	
	//===============================================================================================
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setCharacterEncoding("UTF-8");


		System.out.println("========================");
		String type = request.getParameter("type");
		if (type == null) {
			type = "select";
		}
		System.out.println("您选择的操作是：" + type);

		if (type.equals("select")) {
			System.out.println("===========查询用户操作=============");

			// 创建Jackson插件的ObjectMapper对象
			ObjectMapper mapper = new ObjectMapper();
			// 将一个Java对象转换成JSON
			mapper.writeValue(response.getWriter(), list);

		} else if (type.equals("add")) {
			System.out.println("==========添加用户操作============");
           
			int id = Integer.parseInt(request.getParameter("user_code"));
			String username = request.getParameter("user_name");
			int age = Integer.parseInt(request.getParameter("user_age"));
			String gender = request.getParameter("add_gender");
			String avat = "空";
			String addr = request.getParameter("user_address");
			String email = request.getParameter("user_email");
			User user = new User();
			System.out.println("编号：" + id + "姓名" + username + "年龄：" + age + "性别：" + gender + "头像：" + avat + "地址：" + addr
					+ "邮箱：" + email);
			user.setAge(age);
			user.setAddr(addr);
			user.setUsername(username);
			user.setAvat(avat);
			user.setEmail(email);
			user.setId(id);
			user.setGender(gender);
			list.add(user);
			System.out.println("用户的数量有：" + list.size());
			ObjectMapper mapper = new ObjectMapper();
			// 将一个Java对象转换成JSON
			mapper.writeValue(response.getWriter(), list);
		} else if (type.equals("edit")) {

			System.out.println("=============编辑用操作=================");
			int id = Integer.parseInt(request.getParameter("user_code"));

			User user = new User();
			HashSet<User> hs = list;
			for (User user2 : hs) {
				if (user2.getId() == id) {
					user.setId(id);
					user.setAddr(user2.getAddr());
					user.setUsername(user2.getUsername());
					user.setAge(user2.getAge());
					user.setGender(user2.getGender());
					user.setEmail(user2.getEmail());
					user.setAvat(user2.getAvat());
				}
			}

			ObjectMapper mapper = new ObjectMapper();
			mapper.writeValue(response.getWriter(), user);

		} else if (type.equals("update")) { // 编辑后保存

			System.out.println("==========编辑后保存用户操作============");

			int id = Integer.parseInt(request.getParameter("user_codes"));
			// String id = request.getParameter("user_codes");
			String username = request.getParameter("user_names");
			int age = Integer.parseInt(request.getParameter("user_ages"));

			String gender = request.getParameter("user_genders");

			String avat = "空";
			String addr = request.getParameter("user_addresss");
			String email = request.getParameter("user_emails");
			System.out.println();

			// User user = new User();

			HashSet<User> hs = list;
			for (User user2 : hs) {
				System.out.println(user2);
				if (user2.getId() == id) {
					user2.setAddr(addr);
					user2.setUsername(username);
					user2.setAge(age);
					user2.setGender(gender);
					user2.setEmail(email);
					user2.setAvat(avat);
				}
			}

			System.out.println("用户的数量有：" + list.size());
			ObjectMapper mapper = new ObjectMapper();
			// 将一个Java对象转换成JSON
			mapper.writeValue(response.getWriter(), list);

		} else if (type.equals("delete")) {
			System.out.println("===============删除用户操作============");
			int id = Integer.parseInt(request.getParameter("user_code"));
			HashSet<User> hs = list;
			User user = new User();
			for (User user2 : hs) {
				if (user2.getId() == id) {
					user = user2;
					break;
				}
			}
			list.remove(user);

			ObjectMapper mapper = new ObjectMapper();
			mapper.writeValue(response.getWriter(), list);

		} else if (type.equals("delall")) {
			System.out.println("==============执行了批量删除操作=============");
			String flag = request.getParameter("flag");
			String delIds = request.getParameter("delIds");
			System.out.println("=======" + flag + "==============");
			if (flag.equals("1") && delIds != null) {

				System.out.println(delIds);

				String[] del = delIds.split("\\+");
				System.out.println(del.length);
				for (int i = 1; i < del.length; i++) {
					// 进行删除操作
					if (del[i] != " " || del[i] != null) {
						removeUserById(del[i]);
					}
				}

			} else {

				// 进行字符串的分割，删除指定用户
				String delId = request.getParameter("delId");
				System.out.println(delId);
				String[] del = delId.split("\\+");
				System.out.println(del.length);
				for (int i = 1; i < del.length; i++) {
					// 进行删除操作
					if (del[i] != " " || del[i] != null) {
						removeUserById(del[i]);
					}
				}
			}

			System.out.println("用户的数量有：" + list.size());
			ObjectMapper mapper = new ObjectMapper();
			// 将一个Java对象转换成JSON
			mapper.writeValue(response.getWriter(), list);

		} else {
			System.out.println("========不执行任何操作==========");
		}

	}

	private void removeUserById(String uid) {
		int id = Integer.parseInt(uid);
		HashSet<User> hs = list;
		User user = new User();
		for (User user2 : hs) {
			if (user2.getId() == id) {
				user = user2;
				break;
			}
		}
		list.remove(user);

	}

}
